package providev.learn.imagina.tema2_4;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class DialogEx extends Activity {
    static final private int MY_DIALOG = 1;
    static final private int DATE_DIALOG = 2;
    
    private TextView txtResult;
    
    private int mYear;
    private int mMonth;
    private int mDay;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.main);
   
    	txtResult = (TextView) findViewById(R.id.txtResult);
    	
    	Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
    	
    	Button cmdAction = (Button) findViewById(R.id.cmdAction);
    	cmdAction.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                    showDialog(MY_DIALOG);
            }
    	});
    	
    	Button dateAction = (Button) findViewById(R.id.dateAction);
    	dateAction.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                    showDialog(DATE_DIALOG);
       }
    });

    }
    
    
    @Override
    public Dialog onCreateDialog(int id) {
            switch(id) {
            case MY_DIALOG:
                    AlertDialog.Builder myDialog = new AlertDialog.Builder(this);
                    myDialog.setTitle("This is my title");
                    myDialog.setMessage("");
                    myDialog.setPositiveButton("OK",
                         new DialogInterface.OnClickListener() {
                                 @Override
                                 public void onClick(DialogInterface dialog, int which) {
                                         txtResult.setText("You've chosen OK");
                                 }
                         });
                    myDialog.setNegativeButton("Cancel",
                         new DialogInterface.OnClickListener() {
                                 @Override
                                 public void onClick(DialogInterface dialog, int which) {
                                         txtResult.setText("You've chosen Cancel");
                                 }
                         });
                    return myDialog.create();
            case DATE_DIALOG:            	
		    	return new DatePickerDialog(this,
	                    mDateSetListener,
	                    mYear, mMonth, mDay);
            }
            
            return null;
    }
    
    private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
            	txtResult.setText(
                        new StringBuilder()
                                // Month is 0 based so add 1
                        		.append(dayOfMonth).append("-")
                        		.append(monthOfYear + 1).append("-")                                
                                .append(year).append(" "));
            }
        };
    
    
    
    

    
    @Override
    public void onPrepareDialog(int id, Dialog dialog) {
    	switch(id) {
            case MY_DIALOG:
                    AlertDialog myDialog = (AlertDialog) dialog;
                    myDialog.setMessage("Choose an action");
                    break;
            
	    	case DATE_DIALOG:            	
	    			new DatePickerDialog(this,
	                mDateSetListener,
	                mYear, mMonth, mDay);
	    			break;
	    }
    }
}
